# https://leetcode.cn/problems/jC7MId/description/

class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right


class Solution:
    def __init__(self):
        self.res = -2147483647 - 1

    def maxPathSum(self, root: TreeNode) -> int:
        def sumScore(node: TreeNode) -> int:
            if node is None:
                return 0

            leftScore = max(0, sumScore(node.left))
            rightScore = max(0, sumScore(node.right))
            self.res = max(self.res, leftScore + rightScore + node.val)
            return max(leftScore, rightScore) + node.val
        sumScore(root)
        return self.res
